# -*- coding: utf-8 -*-
"""
sklearn中的多项式回归和Pipeline
Created on Sat Mar 31 16:54:56 2018

@author: Allen
"""
import numpy as np
import matplotlib.pyplot as plt

x = np.random.uniform( -3, 3, size = 100 )
X = x.reshape( -1, 1 )

y = 0.5 * x**2 + x + 2 + np.random.normal( 0, 1, size = 100 )

# sklearn 中的多项式回归
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures( degree = 2 )
poly.fit( X )
X2 = poly.transform( X )
print( X2[:5, :] )
'''
[[ 1.         -1.68461149  2.83791588]
 [ 1.         -0.3381483   0.11434427]
 [ 1.          2.39462016  5.73420569]
 [ 1.          0.50186888  0.25187237]
 [ 1.         -2.04982492  4.20178222]]
可以看出，一共出现3列，第一列相当于添加了一个x的0次方，
第2列，是源数据
第3列，是源数据的2次方
'''
from sklearn.linear_model import LinearRegression
lin_reg2 = LinearRegression()
lin_reg2.fit( X2, y )
y_predict2 = lin_reg2.predict( X2 )

plt.scatter( x, y )
plt.plot( np.sort( x ), y_predict2[ np.argsort( x ) ], color = "r" )
plt.show()

print( lin_reg2.coef_ ) # [ 0.          0.97784913  0.53640751]
print( lin_reg2.intercept_ ) # 1.85352308944

###pipeline 可以将多项式生成、数据归一化、线性回归合三为一
x = np.random.uniform( -3, 3, size = 100 )
X = x.reshape( -1, 1 )

y = 0.5 * x**2 + x + 2 + np.random.normal( 0, 1, size = 100 )

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

poly_reg = Pipeline([
            ( "poly", PolynomialFeatures( degree = 2 ) ),
            ( "std_scaler", StandardScaler() ),
            ( "lin_reg", LinearRegression() )
        ])
poly_reg.fit( X, y )
y_predict = poly_reg.predict( X )

plt.scatter( x, y )
plt.plot( np.sort( x ), y_predict[np.argsort( x )], color = "r" )
plt.show()

'''
以后直接使用pipeline就可以了。非常方便。
'''







